import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
// import { VueAxios } from './axios'
import { Modal, notification } from 'ant-design-vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */

// 创建 axios 实例
const service = axios.create({
  timeout: 300000 // 请求超时时间
})

const fileService = axios.create({
  timeout: 30000 // 请求超时时间
})

const err = (error) => {
  // console.log('error----：', error)
  if (error.response) {
    const that = this
    const data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    switch (error.response.status) {
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问', duration: 4 })
        break
      case 500:
        if (token && data.message === 'Token失效，请重新登录') {
          Modal.error({
            title: '登录已过期',
            content: '很抱歉，登录已过期，请重新登录',
            okText: '重新登录',
            mask: false,
            onOk: () => {
              store.dispatch('Logout').then(() => {
                Vue.ls.remove(ACCESS_TOKEN)
                try {
                  const path = that.$route.path
                  if (path.indexOf('/user/login') === -1) {
                    window.location.reload()
                  }
                } catch (e) {
                  window.location.reload()
                }
              })
            }
          })
        } else {
          notification.error({ message: '系统提示', description: '服务器遇到错误，无法完成请求!', duration: 4 })
        }
        break
      case 404:
        notification.error({ message: '系统提示', description: '很抱歉，资源未找到!', duration: 4 })
        break
      case 504:
        notification.error({ message: '系统提示', description: '网络超时' })
        break
      case 401:
        notification.error({ message: '系统提示', description: '未授权，请重新登录', duration: 4 })
        if (token) {
          store.dispatch('Logout').then(() => {
            setTimeout(() => {
              window.location = ''
              // window.location.reload()
            }, 1500)
          })
        }
        break
      default:
        notification.error({ message: '系统提示', description: data.message, duration: 4 })
        break
    }
  }
  console.log('异常响应：', error)
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  console.log('正常请求：', config)
  return config
}, (error) => {
  console.log('异常请求：', error)
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
  // console.log('原始响应：', response.data)
  if (response.data[Vue.prototype.$response.CODE] === Vue.prototype.$response.SUCCESS) {
    console.log('正常响应：', response.data)
    return response.data
  } else {
    console.log('错误响应：', response.data)
    throw response.data
  }
}, (err))
// const installer = {
//   vm: {},
//   install (Vue, router = {}) {
//     Vue.use(VueAxios, router, service)
//   }
// }

fileService.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  console.log('正常请求：', config)
  return config
}, (error) => {
  console.log('异常请求：', error)
  return Promise.reject(error)
})

fileService.interceptors.response.use((response) => {
  return response
}, err)

export {
  // installer as VueAxios,
  service as axios,
  fileService as fileAxios
}
